package hb.software.role.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import hb.software.role.entity.Role;
import hb.software.service.role.RoleManager;
import edu.zut.hb.software.base.web.GenericController;

@RestController
@RequestMapping("/role")
public class RoleController extends GenericController<Role, Long, RoleManager> {


    RoleManager roleManager;

    @Autowired
    public void setRoleManager(RoleManager roleManager) {
        this.roleManager = roleManager;
        this.manager = this.roleManager;
    }

    @GetMapping("/findall/{page}/{size}")
    public Page<Role> findall(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return roleManager.findAll(pageable);
    }

    @PostMapping("/save")
    public Role save(@RequestBody Role role) {
        Role result = roleManager.save(role);
        return result;
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") Long id) {
        roleManager.delete(id);
    }

    @GetMapping("/findbyid/{id}")
    public Role findById(@PathVariable("id") Long id) {
        return roleManager.findById(id);
    }

    @PutMapping("/updata")
    public Role updata(@RequestBody Role role) {
        Role result = roleManager.save(role);
        return result;
    }
}
